//
//  CtypeViewController.m
//  AlgorithmDemo
//
//  Created by zxs on 2020/8/14.
//  Copyright © 2020 zxs. All rights reserved.
//

#import "CtypeViewController.h"
#import "SingleList.h"
#import "StringUtil.h"
#import "ArrayUtil.h"

@interface CtypeViewController ()

@end

@implementation CtypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 单链表反转
- (IBAction)reverseButtonTouched:(id)sender {
    int array[5] = {1, 2, 3, 4, 5};
    struct Node *list = arrayToList(array, 5);
    printf("一开始的链表:\n");
    printList(list);
    struct Node *newList = reverseList(list);
    printf("反转的链表:\n");
    printList(newList);
}

// 字符串反转
- (IBAction)reverseStringButtonTouched:(id)sender {
    // 通常的做法
    char message[] = "Hello,world!";
    printf("原来的字符串：%s\n", message);
    reverse(message);
    printf("反转后的字符串：%s\n", message);
    
    // 包含中文
    unsigned char s[] = "你好，世界！1234abcdXYZ";
    unsigned char t[300] = {0};
    InverseStr(t, s);
    
    // https://zhidao.baidu.com/question/546085242.html
    printf("\n\n============包含中文========不成功的尝试=====\n\n");
    printf("原来的字符串：%s\n", s);
    printf("反转后的字符串：%s\n", t);
}

// 有序数组合并
- (IBAction)mergeButtonTouched:(id)sender {
    int a[5] = {1,4,6,7,9};
    int b[8] = {2,3,5,6,8,10,11,12};
    
    //// 用于存储归并结果
    int result[13];
    //// 归并操作
    mergeList(a, 5, b, 8, result);
    //// 打印归并结果
    printf("merge result is:\n ");
    for (int i = 0; i < 13; i++) {
        printf("%d ", result[i]);
    }
}

- (IBAction)quickButtonTouched:(id)sender {
    int a[10] = {30, 20, 60, 40, 90, 30, 80, 10, 50, 80};
    int low = 0;
    int high = 9;
    quickSort(a, low, high);
    for (int i = 0; i < 10; i++) {
        printf("%d,", a[i]);
    }
}

@end
